package com.king.armips.android.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

public class SoftKeyBoardUtils {

    public static void hideSoftKeyboard(Context context) {
        if (context == null)
            return;
        if (context instanceof Activity) {
            hideSoftKeyboard((Activity) context);
        }
    }

    public static void hideSoftKeyboard(Activity activity) {
        if (activity == null)
            return;
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        Window window = activity.getWindow();
        if (window == null)
            return;
        View v = window.peekDecorView();
        if (v == null)
            return;
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    /**
     * 显示键盘
     * @param view 输入框view
     */
    public static void showSoftKeyboard(View view) {
        if (view == null)
            return;
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethodManager != null) {
                view.requestFocus();
                inputMethodManager.showSoftInput(view, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏键盘
     * @param view 输入框view
     */
    public static void hideSoftKeyboard(View view) {
        if (view == null)
            return;
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethodManager != null) {
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                view.clearFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
